//
// Created by liushuai on 2020/5/5.
//
#include "bfs_tree.h"
void bfs_queue(TreeNode* root){
    std::queue<TreeNode*> q;
    TreeNode* current_node = root;
    q.push(current_node);
    while (q.size()>0){
        current_node = q.front();
        std::cout<<"访问节点："<<current_node->val;
        q.pop();
        if(current_node->left)
            q.push(current_node->left);
        if(current_node->right)
            q.push(current_node->right);
    }
}
std::vector<std::vector<int>> travel_layer(TreeNode* root){
    std::queue<TreeNode*> q;
    std::vector<std::vector<int>> result;
    if(root)
        q.push(root);
    while (q.size()){
        std::vector<int> layer;
        int node_nums = q.size();//        分层打印的关键是需要指导当前队列中节点的个数（叶子节点的数目）
        for(int i=0;i<node_nums;i++){
            TreeNode* node = q.front();
            q.pop();
            if(node->left)
                q.push(node->left);
            if(node->right)
                q.push(node->right);
            layer.push_back(node->val);
        }

        result.push_back(layer);
    }
    return result;
}


